#include "ufo.h"

UFO::UFO()
{
	speed = 0.3f;
	speedTimer = 0;
	shieldTimer = 0;
	stallTimer = 0;
	moving = false;
	shielded = false;

	//UFO begins in center of screen
	position = Vector2((float)(Iw2DGetSurfaceWidth() / 2), float(Iw2DGetSurfaceHeight() / 2));

	SetupImages();

	boundingBox = BoundingBox(position, width, height);
}

UFO::~UFO()
{
	CleanupImages();
}

void UFO::SetupImages()
{
	ufoSprite = Iw2DCreateImageResource("ufo");
	width  = ufoSprite->GetWidth();
    height = ufoSprite->GetHeight();
	widthHalf = width / 2;
	heightHalf = height / 2;
}

void UFO::CleanupImages()
{
	if(ufoSprite != NULL)
	{
		delete ufoSprite;
		ufoSprite = NULL;
	}
}

void UFO::Render()
{
	Iw2DDrawImage(ufoSprite, CIwSVec2((int16)position.x - widthHalf, (int16)position.y - heightHalf));
}

void UFO::Update(int deltaTimeMs)
{
	if(moving)
	{
		//This is what moves the ufo each frame
		position += direction * (float)(deltaTimeMs * speed);

		//Check if ufo has passed through the destination point to the other side of it while moving
		//If so stop moving it and set position = destination
		if(destination.x > position.x && positionXGreaterThanDestination == true)
		{
			position = destination;
		}
		else if(destination.x < position.x && positionXGreaterThanDestination == false)
		{
			position = destination;
		}
		else if(destination.y > position.y && positionYGreaterThanDestination == true)
		{
			position = destination;
		}
		else if(destination.y < position.y && positionYGreaterThanDestination == false)
		{
			position = destination;
		}

		if(position == destination)
		{
			moving = false;
		}

		boundingBox.UpdatePosition(position);
	}

	if(speedTimer > 0)
	{
		speedTimer -= deltaTimeMs;

		if(speedTimer <= 0)
		{
			//Don't reset the speed to normal if the ship is still stalled by a comet
			if(stallTimer <= 0)
			{
				speed = 0.3f;
			}
		}
	}

	if(shieldTimer > 0)
	{
		shieldTimer -= deltaTimeMs;

		if(shieldTimer <= 0)
		{
			shielded = false;
		}
	}

	if(stallTimer > 0)
	{
		stallTimer -= deltaTimeMs;

		if(stallTimer <= 0)
		{
			speed = 0.3f;
		}
	}
}

void UFO::FlyTo(int x, int y)
{
	destination = Vector2((float)x, (float)y);

	if(position != destination)
	{
		//Direction to move in to reach destination
		direction = destination - position;
		direction.normalize();
		moving = true;

		if(destination.x < position.x)
		{
			positionXGreaterThanDestination = true;
		}
		else
		{
			positionXGreaterThanDestination = false;
		}
		if(destination.y < position.y)
		{
			positionYGreaterThanDestination = true;
		}
		else
		{
			positionYGreaterThanDestination = false;
		}
	}
}

void UFO::FlyTo(Vector2 destination)
{
	this->destination = destination;

	if(position != destination)
	{
		//Direction to move in to reach destination
		direction = destination - position;
		direction.normalize();
		moving = true;

		if(destination.x < position.x)
		{
			positionXGreaterThanDestination = true;
		}
		else
		{
			positionXGreaterThanDestination = false;
		}
		if(destination.y < position.y)
		{
			positionYGreaterThanDestination = true;
		}
		else
		{
			positionYGreaterThanDestination = false;
		}
	}
}

void UFO::ActivateShield()
{
	shielded = true;
	shieldTimer = SHIELD_TIME;
}

void UFO::ActivateSpeed()
{
	speed = 0.5f;
	speedTimer = SPEED_TIME;
}

void UFO::Crash()
{
	if(shielded == false)
	{
		speed = 0.0f;
		stallTimer = STALL_TIME;
	}
}